﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Activity.Data;

namespace Activity.API
{
    public class HappyWeek520Handler : BaseLottery
    {
        /// <summary>
        /// 结束时间
        /// </summary>
         DateTime _baseEndDate = new DateTime(2018, 05, 31, 23, 59, 59);

        /// <summary>
        /// 开始时间
        /// </summary>
         DateTime _baseBeginDate = new DateTime(2018, 05, 18, 0, 0, 0);

        /// <summary>
        /// 抽奖
        /// </summary>
        /// <param name="type"></param>
        /// <param name="openId"></param>
        /// <returns></returns>
        public Messages Lottery(string openId)
        {
            var msg = new Messages();
            if (string.IsNullOrWhiteSpace(openId))
                 msg.Code = 40001;
            if (this.CheckDate(_baseBeginDate, _baseEndDate))
            {
                if (this.CheckUser(openId))
                {
                    var result = CheckUserLottery(openId);
                    if (result == -1)
                    {
                        var map = new Dictionary<string, double>
                        {
                            {"5元夏日清凉券", 0.697},
                            {"10元夏日清凉券", 0.3},
                            {"66.6元夏日清凉券", 0.003}
                        };
                        var index= BuildLoggeryInfo(map);
                        string THQNo = GetTHQNo();
                        string CardCode = GetTHQID(index);
                        try
                        {
                            lock (obj)
                            {
                                //写入ERP奖券
                                SaveRedPacHKERP(openId, CardCode, index, THQNo);
                                //抽奖日志
                                AddRedPacLog(openId, CardCode, 2);
                            }

                            msg.Code = 1;
                            msg.Message = index.ToString();
                        }
                        catch
                        {
                            msg.Code = 40004;
                        }
                    }

                    msg.Code = 2;
                    msg.Message = result.ToString();
                }
                msg.Code = 40003;
            }

            msg.Code = 40002;

            return msg;
        }

        /// <summary>
        /// 检测用户是否已抽奖
        /// </summary>
        /// <param name="openId"></param>
        /// <returns></returns>
        private int CheckUserLottery(string openId)
        {
            //限制用户一天只领取一次
            var dc = new ActivityEntities();
           var result= dc.KM_HappyNewYearLog.FirstOrDefault(t => t.OpenId == openId);
            if (result != null)
                return result.CardType;
            return -1;
        }

        /// <summary>
        /// 根据红包类型获取三种红包的固定ERP内码
        /// </summary>
        /// <param name="CardType">卷类型：1=5、2=10、3=66.6</param>
        /// <returns></returns>
        public string GetTHQID(int CardType)
        {
            string THQID = string.Empty;
            if (CardType == 2)
            {
                THQID = "KM201819";
            }
            else if (CardType == 3)
            {
                THQID = "KM201820";
            }
            else
            {
                THQID = "KM201818";
            }
            return THQID;
        }

        /// <summary>
        /// 保存已领红包系统日志
        /// </summary>
        /// <param name="OpenId">OpenId</param>
        /// <param name="CardCode">红包卡编号</param>
        /// <param name="CardType">红包类型</param>
        public void AddRedPacLog(string OpenId, string CardCode, int CardType)
        {
            try
            {
                using (var dc = new ActivityEntities())
                {
                    var addLog = new KM_HappyNewYearLog
                    {
                        SID = Guid.NewGuid().ToString(),
                        OpenId = OpenId,
                        CardCode = CardCode,
                        CardType = CardType,
                        ReceiveTime = DateTime.Now,
                        ActivityType = CardType
                    };
                    dc.KM_HappyNewYearLog.Add(addLog);
                    dc.SaveChanges();
                };
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
